import Datastore from 'lowdb'
import LodashId from 'lodash-id'
import FileSync from 'lowdb/adapters/FileSync'
import path from 'path'
import fs from 'fs-extra'

// 获取electron应用的用户目录
const os = require('os');
const STORE_PATH = path.join(os.homedir(), '.Ishare');

// 如果路径不存在, 则创建
if(!fs.pathExistsSync(STORE_PATH)) {
  fs.mkdirpSync(STORE_PATH)
}

// 初始化lowdb读写的json文件名以及存储路径
const adapter = new FileSync(path.join(STORE_PATH, '/data.json'));

// lowdb接管该文件
const db = Datastore(adapter);
db._.mixin(LodashId);

if (!db.has('file_share').value()) {
  db.set('file_share', []).write();
}
if (!db.has('text_share').value()) {
  db.set('text_share', []).write();
}

export default db
